package com.lijin.study.java.quartz;

import java.util.Date;

import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzTestMain {
	
	public void simpleTriggerRunner() throws SchedulerException {
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler = schedulerFactory.getScheduler();
		
		JobDetail jobDetail = new JobDetail("JobDetail-1", "JobDetailGroup-1", SimpleQuartzJob.class);
		Trigger trigger = TriggerUtils.makeSecondlyTrigger(3);
		trigger.setName("trigger-1");
		trigger.setStartTime(new Date(System.currentTimeMillis() + 5000));
		scheduler.scheduleJob(jobDetail, trigger);
		scheduler.start();
	}
	
	public void cronTriggerRunner() throws Exception {
		SchedulerFactory factory = new StdSchedulerFactory();
		Scheduler scheduler = factory.getScheduler();
		
		JobDetail jobDetail = new JobDetail("jobDetail2", "jobDetailGroup2", SimpleQuartzJob.class);
		CronTrigger cronTrigger = new CronTrigger("cronTrigger", "triggerGroup2");
		try {
            CronExpression cexp = new CronExpression("0/1 * * * * ?");
            cronTrigger.setCronExpression(cexp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        scheduler.scheduleJob(jobDetail, cronTrigger);
        
        scheduler.start();
	}
	
	public static void main(String[] args) throws Exception {
		QuartzTestMain m = new QuartzTestMain();
		m.cronTriggerRunner();
	}
}
